package seatechit.msbgateway.dbaccess.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import seatechit.msbgateway.dbaccess.SpringFrameworkDAO;
import seatechit.msbgateway.dbaccess.dao.SalaryDAO;
import seatechit.msbgateway.dbaccess.entity.SalaryBKDDSALW;
import seatechit.msbgateway.dbaccess.entity.SalaryBKDDSALWH;

public class SalaryDAOImpl extends SpringFrameworkDAO implements SalaryDAO {

	public boolean saveSalary(ArrayList<SalaryBKDDSALW> arrSalaryBKDDSALW, ArrayList<SalaryBKDDSALWH> arrSalaryBKDDSALWH) {
		Connection connToSIBS = null;
		boolean result = true;
		PreparedStatement pst1 = null;
		PreparedStatement pst2 = null;
		try {
			connToSIBS = getDb2CoreJDBC("10.0.1.1", "salarydele", "salarydele");
			connToSIBS.setAutoCommit(false);
			if (arrSalaryBKDDSALW != null && arrSalaryBKDDSALWH != null) {
				// DDSALW
				for (int i = 0; i < arrSalaryBKDDSALW.size(); i++) {

					SalaryBKDDSALW objSqlW = arrSalaryBKDDSALW.get(i);
					if (result) {
						String insBKDDSALW = "INSERT INTO stdattrn.ddsalw VALUES('" + objSqlW.getSDID() + "'," + objSqlW.getSDBRN() + ",'" + objSqlW.getSDRCID() + "','"
								+ objSqlW.getSDCTYP() + "'," + objSqlW.getSDSEQ() + ",'" + objSqlW.getSDACCT() + "',0,0,'" + objSqlW.getSDATYP() + "',0," + objSqlW.getSDAMT()
								+ ",0,0,'" + objSqlW.getSDMUID() + "','" + objSqlW.getSDMWID() + "'," + objSqlW.getSDMTIM() + "," + objSqlW.getSDDAT6() + "," + objSqlW.getSDDAT7()
								+ ",' ','" + objSqlW.getREMRK1() + "' ,'" + objSqlW.getREMRK2() + " ',' ',' ')";

						pst1 = connToSIBS.prepareStatement(insBKDDSALW);
						int ps1Rows = pst1.executeUpdate();
						// if (ps1Rows == 0)
						// result = false;
					}
				}

				// DDSALWH
				if (result) {
					for (int j = 0; j < arrSalaryBKDDSALWH.size(); j++) {

						SalaryBKDDSALWH objSqlW = arrSalaryBKDDSALWH.get(j);
						String insBKDDSALWH = "INSERT INTO stdattrn.ddsalwh VALUES('" + objSqlW.getSMID() + "','" + objSqlW.getSMRCID() + "','" + objSqlW.getSMACCT() + "',0,0,'"
								+ objSqlW.getSMATYP() + "'," + objSqlW.getSMBRN() + ",'" + objSqlW.getSMCTYP() + "',0," + objSqlW.getSMAMT() + ",0,0,0,0,'" + objSqlW.getSSMUID()
								+ "','" + objSqlW.getSSMWID() + "'," + objSqlW.getSSMTIM() + "," + objSqlW.getSMDAT6() + "," + objSqlW.getSMDAT7() + "," + objSqlW.getSMTFR6()
								+ "," + objSqlW.getSMTFR7() + ",0,0,' ',' ','" + objSqlW.getSMRMK1() + " ',' " + objSqlW.getSMRMK2() + "')";
						pst2 = connToSIBS.prepareStatement(insBKDDSALWH);
						int ps2Rows = pst2.executeUpdate();
						// if (ps2Rows == 0)
						// result = false;
					}
				}
				// Commit
				connToSIBS.commit();
			} else {
				result = false;
			}
		} catch (SQLException e) {
			if (connToSIBS != null) {
				try {
					connToSIBS.rollback();
				} catch (SQLException excep) {
					excep.printStackTrace();
				}
			}

		} finally {
			try {
				if (pst1 != null) {
					pst1.close();
				}
				if (pst2 != null) {
					pst2.close();
				}
				connToSIBS.setAutoCommit(true);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
}
